import {Component, OnInit} from '@angular/core';
import {CommonService} from "../../service/base/common.service";
import {NzModalService} from "ng-zorro-antd/modal";
import {HttpCustomConfig} from "../../service/base/base.http.service";
import {ProductHttpService} from "../../service/http/product.http.service";
import {UserDetailComponent} from "../../dialog/user-detail/user-detail.component";
import {ProductStockComponent} from "../../dialog/product-stock/product-stock.component";

@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.less']
})
export class ProductComponent implements OnInit {

  dataList: any[] = [];

  constructor(private commonService: CommonService,
              private modalService: NzModalService,
              private productHttpService: ProductHttpService) {
  }

  defaultHttpConfig: HttpCustomConfig = {showLoading: false}

  ngOnInit() {
    this.queryData()
  }

  queryData(): void {
    this.productHttpService.queryProduct({productType: 10}, this.defaultHttpConfig).subscribe((res) => {
      this.dataList = res.data.dataList
    })
  }

  updateProduct(data: any): void {
    this.productHttpService.updatePrice({
      productId: data.productId,
      unitPrice: data.unitPriceDisplay,
      memberPrice: data.memberPriceDisplay,
    }, this.defaultHttpConfig).subscribe((res) => {
      this.productHttpService.updateDesc({
        productId: data.productId,
        productDesc: data.productDesc
      }, this.defaultHttpConfig).subscribe((res) => {
        this.queryData();
      })
    })
  }

  updateStatus(data: any): void {
    this.productHttpService.updateStatus({
      productId: data.productId,
    }, this.defaultHttpConfig).subscribe((res) => {
      this.queryData();
    })
  }

  updateStock(data: any): void {
    this.modalService.create({
      nzTitle: '修改库存',
      nzMaskClosable: false,
      nzContent: ProductStockComponent,
      nzData: {productId: data.productId},
      nzFooter: null,
    }).afterClose.subscribe((): void => {
      this.queryData();
    });
  }
}
